/*
Module Name:  ScriptedGame.cs
Project:      ScriptManager
Copyright (C) 2012 Felipe Ramos

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ScriptManager.Scripting;

namespace ScriptManager
{
#if WINDOWS || XBOX
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            using (ScriptedGame game = new ScriptedGame())
            {
                game.Run();
            }
        }
    }
#endif

    /// <summary>
    /// Main game object leveraging our scriptable framwork.
    /// </summary>
    public class ScriptedGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        /// <summary>
        /// Scriptable level variable, because of the level encapsulation the footprint in
        /// the main game object is minimal.
        /// </summary>
        Level _level;    

        public ScriptedGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // Create level instance
            _level = new Level("Content/level1.script");
            _level.Load(Content);

            // Set game properties defined for the current level.
            Window.Title = _level.Values[LevelKeys.title];
            graphics.PreferredBackBufferHeight = Convert.ToInt32(_level.Values[LevelKeys.height]);
            graphics.PreferredBackBufferWidth = Convert.ToInt32(_level.Values[LevelKeys.width]);
            graphics.ApplyChanges();
        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // Update level.
            _level.Update(gameTime);    

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // Draw level.
            _level.Draw(gameTime, spriteBatch);

            base.Draw(gameTime);
        }
    }
}
